/**
 * 选择用户的控件
 */
$(document).ready(function(){
	var $select = $('#selectTagsAjax');
	//获取当前已经选中的，并初始化到select中
	$('input[name="selectTagsAjaxSelected"]').each(function(){
		var _this = $(this);
		$select.append("<option value='"+_this.attr('value')+"' selected>"+_this.attr('label')+"</option>");
		_this.remove();//删除，以免产生不必要的影响
	});
	//再使用功能实现
	if ($select.data('selectator') === undefined) {
		var s = $select.selectator({
			showAllOptionsOnFocus: true,
			selectFirstOptionOnSearch: true,
			load: function (search, callback) {
				if (search.length < this.minSearchLength) return callback();
				$.ajax({
					url: '/tags/search?q='+encodeURIComponent(search),
					type: 'GET',
					dataType: 'json',
					success: function(data) {
						if(data){
							callback(data);
						}
					},
					error: function() {
						callback();
					}
				});
			},
			placeholder: '输入标签进行筛选',
			delay: 500,
			minSearchLength: 0,
			valueField: 'tagId',
			textField: 'name',
			render: {
				option: function (_item, escape) {
					var tagName = (typeof _item.name !== 'undefined') ? _item.name : ((typeof _item.text !== 'undefined') ? _item.text : '');
					var html = '';
					html += '<div class="selectator_option_title">' + tagName + '</div>';
					//<a href="javascript:void(0);" onclick="dele('+(_item.tagId?_item.tagId:_item.value)+')" style="margin-left:5px;z-index:9999;">删除</a>
					return html;
				}
			}
		});
	}
});

function dele(tagId){
	alert(tagId);
}